<?php
/**
 * @author Huynh.Phan
 */
class ARedisString extends ARedisEntity
{
    protected $_value;
    
    public function __toString()
    {
		return $this->getValue();
	}
    
    public function clear()
    {
		$this->_value = null;
		$this->getConnection()->getClient()->delete($this->name);
		return $this;
	}

    public function getValue($forceRefresh = false)
    {
        if ($this->_value === null || $forceRefresh) {
			if ($this->name === null) {
				throw new CException(get_class($this)." requires a name!");
			}
			$this->_value = (int) $this->getConnection()->getClient()->get($this->name);
		}
		return $this->_value;
    }
    
    public function setValue($value) 
    {
        if ($this->name === null) {
			throw new CException(get_class($this)." requires a name!");
		}
        $this->_value = $value;
        return !$this->getConnection()->getClient()->set($this->name, $value);
    }
    
    public function appendValue($value)
    {
        return $this->setValue($this->_value.$value);
    }
    
    public function increment($byAmount = 1)
    {
        if ($this->name === null) {
			throw new CException(get_class($this)." requires a name!");
		}
        return $this->_value = (int) $this->getConnection()->getClient()->incrBy($this->name,$byAmount);
    }
    
    public function decrement($byAmount = 1)
    {
        if ($this->name === null) {
			throw new CException(get_class($this)." requires a name!");
		}
        return $this->_value = (int) $this->getConnection()->getClient()->decrBy($this->name,$byAmount);
    }
}